package com.zx.ishopping.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

/**
 * jwt令牌加密
 * @author 祝鑫
 * @date 2025/6/22
 */

public class JwtUtil {
    //创建加密方式
    private static final String JWT_SECRET = "com.zx/nb";

    //获取业务数据生成token令牌
    public static String getToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("claims",claims)
                .withExpiresAt(new Date(System.currentTimeMillis()+1000*60*60))
                .sign(Algorithm.HMAC256(JWT_SECRET));
    }

    //接收到token令牌，解密并返回业务数据
    public static Map<String,Object> getTokenData(String token){
        return JWT.require(Algorithm.HMAC256(JWT_SECRET))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }
}
